package com.itluma.isWOak.controller;

import com.github.pagehelper.PageInfo;
import com.itluma.isWOak.pojo.Cost;
import com.itluma.isWOak.service.CostService;
import com.itluma.isWOak.vo.ResultInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Controller
@RequestMapping("/fee")
public class CostController {
    @Autowired
    private CostService costService;
    @GetMapping("/add")
    public String addPage(){
        return "fee/fee_add";
    }
    @PostMapping("/add")
    public String add(Model model, Cost cost){
        costService.add(cost);
        return "redirect:/fee/fee_list";
    }
    @GetMapping("/check")
    @ResponseBody
    public ResultInfo check(String name, Integer costId){
        ResultInfo resultInfo = new ResultInfo( 0,"资费名可用",null);
        if(name == null || name.trim().length() == 0){
            return new ResultInfo(1,"资费名不能为空",null);
        }
        Cost cost = costService.checkName(name);
        if(cost != null && cost.getCostId() != costId){
            resultInfo.setCode(1);
            resultInfo.setMessage("资费名不可用");
        }
        return resultInfo;
    }
    @GetMapping("/fee_list")
    public String list(Model model, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize", defaultValue = "2") Integer pageSize, String field, String sort){
        String orderBy = null;
        if((field == null || field.trim().length()==0) || (sort == null || sort.trim().length()==0)){
            orderBy = "COST_ID ASC";
        }else {
            orderBy = field + " " + sort.substring(5);
        }
        PageInfo<Cost> pageInfo = costService.page(pageNum, pageSize, orderBy);

        model.addAttribute("field", field);
        model.addAttribute("sort", sort);
        model.addAttribute("pageInfo", pageInfo);
        return "fee/fee_list";
    }

    @GetMapping("/detail")
    public String detail(Integer costId, Model model){
        Cost cost = costService.showDetail(costId);
        model.addAttribute("cost", cost);
        return "fee/fee_detail";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public ResultInfo delete(Integer costId){
        Boolean delete = costService.delete(costId);
        ResultInfo resultInfo = new ResultInfo(0,"删除成功！",null);
        if(!delete){
            resultInfo = new ResultInfo(1,"删除失败！不能删除已启用的资费！",null);
        }
        return resultInfo;
    }

    @RequestMapping("/enable")
    @ResponseBody
    public ResultInfo enable(Integer costId){
        Boolean enable = costService.enable(costId);
        ResultInfo resultInfo = new ResultInfo(0,"启用成功！",null);
        if(!enable){
            resultInfo = new ResultInfo(1,"操作失败，该资费已被启用",null);
        }
        return resultInfo;
    }

    @GetMapping("/modi")
    public String showModi(Model model, Integer costId){
        Cost cost = costService.showDetail(costId);
        model.addAttribute("cost", cost);
        return "fee/fee_modi";
    }
    @PostMapping("/modi")
    public String modify(Cost cost, Model model){
        Boolean flag = costService.modify(cost);
        if(flag){
            return "redirect:/fee/list";
        }else{
            model.addAttribute("cost", cost);
            return "fee/fee_modi";
        }
    }
}
